
/*
clear  
set more off
set linesize 255
set autotabgraphs on
version 14
*/

** change directories ** 
*cd "paste filepath of working directory here in parenthesis"
	use replication_data_kamchatka.dta, replace
	label var invest "Allocation to the group account"
	label var amtreceived "Amount received from sharing"

xtset sub_id round

	****************************************
	** TABLE 1 -- SUBJECT CHARACTERISTICS **
	****************************************

	** number of subjects
		table treatment if round == 5 & invest~=., c()
	** gender
		tabulate gender if round == 5 & invest~=.
	** age 
		summarize age if round == 5 & invest~=.
	** indigenous
		tabulate indigenous if round == 5 & invest~=.
	** education
		summarize yrs_educ if round == 5 & invest~=.


	************************************
	** TABLE 2 -- EXPERIMENTAL DESIGN **
	************************************
	table treatment if round == 5 & invest~=., c() 
	table treatment if round == 6 & invest~=., c() row
	

	*****************************
	** TABLE 3 - SUMMARY STATS **
	*****************************
	table treatment_cont if round<=5, contents(mean invest sd invest ) format(%9.1f)
	table treatment_cont if round>=6, contents(mean invest sd invest ) format(%9.1f)
	table treatment_cont if round>=6 & treatment==1, contents(mean amtreceived sd amtreceived ) format(%9.1f)
	table treatment_cont shocked if round>=6, contents(mean amtreceived sd amtreceived ) format(%9.1f)



	*******************************
	** TABLE 4 - AMOUNT RECEIVED **
	*******************************

	*** ERRORS CLUSTERED AT GROUP LEVEL ****
			estimates clear
				eststo: xtreg amtreceived           l.sharing               invest                round if treatment == 1, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 2, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 3, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 4, re vce(cluster session_group)

				esttab using "Table4.rtf" , label cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) starlevels(* .10 ** .05 *** .01) ///
						stats(N, fmt(%9.0f) labels("N"))  ///
						mlabels("Risk" "Reward" "Risk/Reward" "Reputation") collabels(none) ///
						addnotes("Robust standard errors are clustered at the group-level.") ///
						title("Table 4a. Individual Sharing Amount Received") replace	
						
			estimates clear
				eststo: xtreg amtreceived           l.sharing               invest                round age gender indigenous yrs_educ community2 community3 if treatment == 1, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round age gender indigenous yrs_educ community2 community3 if treatment == 2, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round age gender indigenous yrs_educ community2 community3 if treatment == 3, re vce(cluster session_group)
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round age gender indigenous yrs_educ community2 community3 if treatment == 4, re vce(cluster session_group)

				esttab using "Table4b.rtf" , label cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) starlevels(* .10 ** .05 *** .01) ///
						stats(N, fmt(%9.0f) labels("N"))  ///
						mlabels("Risk" "Reward" "Risk/Reward" "Reputation") collabels(none) ///
						addnotes("Robust standard errors are clustered at the group-level.") ///
						title("Table 4. Individual Sharing Amount Received") replace	
						
	*** ERRORS CLUSTERED AT individual LEVEL ****

			estimates clear
				eststo: xtreg amtreceived           l.sharing               invest              round if treatment == 1, re r
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 2, re r
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 3, re r
				eststo: xtreg amtreceived shocked  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 4, re r

				esttab using "Table2_inderrors.rtf" , label cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) starlevels(* .10 ** .05 *** .01) ///
						stats(N, fmt(%9.0f) labels("N"))  ///
						mlabels("T1" "T2" "T3" "T4") collabels(none) ///
						addnotes("Robust standard errors are clustered at the individual level.") ///
						title("Table 4b. Individual Sharing Amount Received-se clustered at ind level") replace	


	******************************************
	** Table 5 Allocation to Group Activity **
	******************************************

	** CHECK FOR TREATMENT EFFECTS IN STAGE 1 (mentioned just before Table 5.)
	
	xtreg invest ib(1).treatment_cont round gender age indigenous yrs_educ community2 community3 if round<=5, re vce(cluster session_group)
	test _b[2.treatment] = _b[3.treatment] = _b[4.treatment] = 0


			estimates clear
				eststo: xtreg invest ib(1).treatment round baselinemeaninvest              					            if treatment>0, re vce(cluster session_group)
				eststo: xtreg invest ib(1).treatment round baselinemeaninvest gender age indigenous yrs_educ community2 community3 	if treatment>0, re vce(cluster session_group)
				esttab using "Table5.rtf" , label cells(b(star fmt(%9.3f)) se(par fmt(%9.2f)) one) starlevels(* .10 ** .05 *** .01) ///
					stats(N, fmt(%9.0f) labels("N"))  ///
					mlabels("Model 1" "Model 2") collabels(none) ///
					addnotes("Robust standard errors are clustered at the group-level.") ///
					title("Table 5. Individual Allocation to Group Activity") replace	

				
				test _b[1.treatment] = _b[2.treatment]
				test _b[1.treatment] = _b[3.treatment]
				test _b[1.treatment] = _b[4.treatment]
				test _b[2.treatment] = _b[3.treatment]
				test _b[2.treatment] = _b[4.treatment]
				test _b[3.treatment] = _b[4.treatment]
				test _b[2.treatment] = _b[3.treatment] = _b[4.treatment] 
				test _b[2.treatment] = _b[3.treatment] = _b[4.treatment] = 0
				test _b[1.treatment] = _b[2.treatment] = _b[3.treatment] = _b[4.treatment] 
				test _b[1.treatment] = _b[2.treatment] = _b[3.treatment] = _b[4.treatment] = 0


	*********************************
	** FIGURE 1 -- AMOUNT RECEIVED **
	*********************************
	preserve
	replace shocked=0 if treatment==1
	collapse (mean) amtreceived if round>=6, by(treatment round shocked)
	gen onewhite=1
	twoway (connected amtreceived round if treatment==1 & !shocked, sort mcolor(blue) lcolor(blue) msymbol(circle))	(connected onewhite round if treatment==1 & !shocked, sort mcolor(white) lcolor(white)) (connected amtreceived round if treatment==2 & !shocked, sort mcolor(green) lcolor(green) msymbol(triangle) lpattern(dash))	(connected amtreceived round if treatment==2 &  shocked, sort mcolor(green) lcolor(green) msymbol(triangle) lpattern(solid))	(connected amtreceived round if treatment==3 & !shocked, sort mcolor(purple) lcolor(purple) msymbol(square_hollow) lpattern(dash))	(connected amtreceived round if treatment==3 &  shocked, sort mcolor(purple) lcolor(purple) msymbol(square_hollow) lpattern(solid))		(connected amtreceived round if treatment==4 & !shocked, sort mcolor(red) lcolor(red) msymbol(lgx) lpattern(dash)) 	(connected amtreceived round if treatment==4 &  shocked, sort mcolor(red) lcolor(red) lpattern(solid) msymbol(lgx)) 	if round>5, ytitle("Mean Amount Received") ylabel(0(50)350, angle(horizontal))  graphregion(color(white)) bgcolor(white) xlabel(1(1)13) xtitle(Round) legend(order(1 "T1. Reward" 2 " " 3 "T2. Risk (no shock)" 4 "T2. Risk (shock)" 5 "T3. Risk/Reward (no shock)" 6 "T3. Risk/Reward (shock)" 7 "T4. Reputation (no shock)" 8 "T4. Reputation (shock)") size(small)) name(figure1, replace) 
	restore
	
	
	********************************
	** FIGURE 2 -- GROUP ACTIVITY **
	********************************
		gen inv_T1_stg2 = .
		gen inv_T2_stg2 = .
		gen inv_T3_stg2 = .
		gen inv_T4_stg2 = .
		
		replace inv_T1_stg2  = invest if treatment_cont == 1
		replace inv_T2_stg2  = invest if treatment_cont == 2
		replace inv_T3_stg2  = invest if treatment_cont == 3
		replace inv_T4_stg2 = invest if treatment_cont == 4

		egen mean_iT1_stg2  = mean(inv_T1_stg2), by(round)
		egen mean_iT2_stg2  = mean(inv_T2_stg2), by(round)
		egen mean_iT3_stg2  = mean(inv_T3_stg2), by(round)
		egen mean_iT4_stg2 = mean(inv_T4_stg2), by(round)

		gen inv_T0 = .
		gen inv_T1 = .
		gen inv_T2 = .
		gen inv_T3 = .
		gen inv_T4 = .
		gen inv_share = .
		
				replace inv_T0 = invest if treatment == 0
				replace inv_T1 = invest if treatment == 1
				replace inv_T2 = invest if treatment == 2
				replace inv_T3 = invest if treatment == 3
				replace inv_T4 = invest if treatment == 4
				replace inv_share = invest if treatment ~= 0
	
	egen mean_iT0 = mean(inv_T0), by(round)
		egen mean_iT1 = mean(inv_T1), by(round)
		egen mean_iT2 = mean(inv_T2), by(round)
		egen mean_iT3 = mean(inv_T3), by(round)
		egen mean_iT4 = mean(inv_T4), by(round)
		egen mean_ishare_all = mean(inv_share), by(round)
	

	twoway  (connected mean_iT1_stg2 round if  round <=5, sort mcolor(blue) lcolor(blue) msymbol(circle)) (connected mean_iT2_stg2 round if  round <=5, sort mcolor(green) lcolor(green) msymbol(triangle)) (connected mean_iT3_stg2 round if  round <=5, sort mcolor(purple) lcolor(purple) msymbol(square_hollow)) (connected mean_iT4_stg2 round if  round <=5, sort mcolor(red) lcolor(red) msymbol(lgx)) (connected mean_iT1 round, sort mcolor(blue) lcolor(blue) msymbol(circle)) (connected mean_iT2 round, sort mcolor(green) msymbol(triangle) lcolor(green)) (connected mean_iT3 round , sort mcolor(purple) msymbol(square_hollow) lcolor(purple)) (connected mean_iT4 round, sort mcolor(red) msymbol(lgx) lcolor(red)),  graphregion(color(white)) bgcolor(white) ytitle(Mean Group Activity) xtitle(Round) ylabel(0(10)50, angle(horizontal)) xlabel(1(1)13) legend(order(1 "T1. Reward" 2 "T2. Risk" 3 "T3. Risk/Reward" 4 "T4. Reputation")) name(figure2, replace)

	


	***************
	**FIGURE 3. individual amount received in sharing, conditioned on whether the individual received a shock, using coefficients in Table 4.
	***************
	** T1 ***
		xtreg amtreceived           l.sharing               invest              round if treatment == 1, re vce(cluster session_group)
			margins, at(invest=(0(1)50)) vsquish saving(fig1, replace)
			marginsplot, noci plotopts(msize(vtiny) mcolor(blue) lwidth(thick) lcolor(blue) lpattern(solid) ylabel(0(100)500) ) name(fig1, replace) saving(fig1, replace)
	**T3**
		xtreg amtreceived  cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 3, re vce(cluster session_group)
			margins, at(invest=(0(1)50) shocked=(0 1)) vsquish saving(fig3, replace)
			marginsplot, noci plot1opts(msize(vtiny) mcolor(purple) lwidth(thick) lcolor(purple) lpattern(dash) ylabel(0(100)500)  ) plot2opts(msize(vtiny) mcolor(purple) lwidth(thick) lcolor(purple) lpattern(solid) ylabel(0(100)500) )  name(fig3, replace) saving(fig3, replace)
	**T4**
		xtreg amtreceived cl.sharing##ib0.shocked c.invest##ib0.shocked  round if treatment == 4, re vce(cluster session_group)
			margins, at(invest=(0(1)50) shocked=(0 1)) vsquish saving(fig4, replace)
			marginsplot, noci plot1opts(msize(vtiny) mcolor(red) lwidth(thick) lcolor(red) lpattern(dash) ylabel(0(100)500)  ) plot2opts(msize(vtiny) mcolor(red) lwidth(thick) lcolor(red) lpattern(solid) ylabel(0(100)500) )  name(fig4, replace) saving(fig4, replace)
			
		*Combining Graphs
		*ssc install combomarginsplot // use if you haven't installed the "combomarginsplot" function


		combomarginsplot fig3 fig4 fig1, ///
			plot1opts(msize(vtiny) mcolor(purple) lwidth(thick) lcolor(purple) lpattern(dash) ylabel(0(100)500)  ) ///
			plot2opts(msize(vtiny) mcolor(red) lwidth(thick) lcolor(red) lpattern(dash) ylabel(0(100)500) ) ///
			plot3opts(msize(vtiny) mcolor(purple) lwidth(thick) lcolor(purple) lpattern(solid) ylabel(0(100)500)  ) ///
			plot4opts(msize(vtiny) mcolor(red) lwidth(thick) lcolor(red) lpattern(solid) ylabel(0(100)500) ) ///
			plot5opts(msize(vtiny) mcolor(blue) lwidth(thick) lcolor(blue) lpattern(solid) ylabel(0(100)500) ) ///
			recastci(rarea) ciopts(color(gs15)) noci ///
			graphregion(color(white)) ytitle("Amount Received via Sharing") xtitle("Allocation to the group activity") title("")  ///
			legend(colfirst) ///
			name(t341, replace) saving(t341, replace)



		** with confidence intervals **
			combomarginsplot fig3 fig4 fig1
		


